package com.wiseman.runner;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class AddressContentHandler extends DefaultHandler {
	
	final String STATUS_TAG = "status";
	final String STATUS_OK = "OK";
	final String ADDRESS_TAG = "formatted_address";
	boolean mIsOK = false;	
	
	enum ReadStatus{
		None,
		Status_tag,
		Address_tag,
		
	};
	
	ReadStatus mStatus = ReadStatus.None;
	private String mAddressStr = null;
	

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		if(mAddressStr != null){
			return;
		}
		
		if(localName.equals(STATUS_TAG)){
			mStatus = ReadStatus.Status_tag;
		}else if(ADDRESS_TAG.equals(localName)){
			mStatus = ReadStatus.Address_tag;
		}
		
		super.startElement(uri, localName, qName, attributes);
		
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if(mAddressStr != null){
			return;
		}
		
		mStatus = ReadStatus.None;
	
		super.endElement(uri, localName, qName);
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(mAddressStr != null){
			return;
		}
		
		if(mStatus == ReadStatus.Status_tag){
			String data = new String(ch, start, length);
			if(STATUS_OK.equals(data)){
				Log.i("address contenthandle", "status ok");
				mIsOK = true;
			}
		}else if(mStatus == ReadStatus.Address_tag && mIsOK){
			mAddressStr  = new String(ch, start, length);
			Log.i("address contenthandle", mAddressStr);			
		}
		
		
		super.characters(ch, start, length);
		
	}
	
	public String getAddress(){
		return mAddressStr;
	}	
	

}
